package traveler.activities;

import traveler.controllers.SocialController;
import traveler.models.Step;
import traveler.tools.ExtendedAlertDialog;
import traveler.tools.ImageAdapter;
import traveler.activities.R;
import traveler.activities.CourseAddActivity.StepInterface;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SocialActivity extends DashboardActivity {
	private Gallery _gallery = null;
	private SocialController _controller = null;
	private TextView _name = null;
	private TextView _description = null;
	private TextView _step = null;
	private RatingBar _rate = null;
	private LinearLayout _list_comments = null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.social);
		setTitleFromActivityLabel(R.id.title_text);

		_controller = new SocialController(this);

		_name = (TextView) findViewById(R.id.social_tx_name_value);
		_description = (TextView) findViewById(R.id.social_tx_description_value);
		_step = (TextView) findViewById(R.id.social_tx_step_value);
		_rate = (RatingBar) findViewById(R.id.social_ratingbar);

		_gallery = (Gallery) findViewById(R.id.social_gallery);
		_gallery.setAdapter(new ImageAdapter(this));

		_gallery.setOnItemClickListener(_controller);
		
//		_list_comments = (LinearLayout) this.findViewById(
//                R.id.social_add_list_comments);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		boolean r = false;
		switch (item.getItemId()) {
		case R.id.menu_comment:
			ExtendedAlertDialog dialog = new ExtendedAlertDialog(1, this);

			dialog.setTitle(this.getResources().getString(
					R.string.comment_title));
			View view = ((LayoutInflater) this
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
					.inflate(R.layout.comment, null);

			dialog.setButton(Dialog.BUTTON_NEGATIVE, "Cancel", this._controller);
			dialog.setButton(Dialog.BUTTON_POSITIVE, "Add", this._controller);

			dialog.setView(view);
			dialog.show();
			r = true;
			break;
		case R.id.menu_course:
			Intent intent = new Intent(this, CourseAddActivity.class);
			Bundle bundle = new Bundle();
			
			
			this.startActivity(intent.putExtras(bundle));
			r = true;
			break;
		default:
			r = super.onOptionsItemSelected(item);
			break;
		}

		return r;
	}

	public TextView get_name() {
		return _name;
	}

	public void set_name(TextView _name) {
		this._name = _name;
	}

	public TextView get_description() {
		return _description;
	}

	public void set_description(TextView _description) {
		this._description = _description;
	}

	public RatingBar get_rate() {
		return _rate;
	}

	public void set_rate(RatingBar _rate) {
		this._rate = _rate;
	}

	public TextView get_step() {
		return _step;
	}

	public void set_step(TextView _step) {
		this._step = _step;
	}
	
	public class StepInterface extends LinearLayout {
        /** Identificateur du bouton Supprimer */
        public static final int BUTTON_DELETE = 1;
        /**  Identificateur du clic sur le texte */
        public static final int ACTION_TEXT = 2;
        /** Identificateur du bouton clock */
        public static final int DELAY_CLOCK = 3;

        /** TextView Nom de la prise */
        private TextView plugName = null;
        /** TextView texte correspondant ˆ l'action */
        private TextView actionText;
        /** Bouton Supprimer */
        private Button delete = null;
        /** RelativeLayout Nom de la prise */
        private RelativeLayout plug = null;
        /**
         * Constructeur
         * @param parent
         * @param step
         */
        public StepInterface(SocialActivity parent, Step step) {
            super(parent);
            this.setLayoutParams(new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT));
            this.setOrientation(LinearLayout.VERTICAL);

            

            RelativeLayout.LayoutParams lp = null;
            this.plug = new RelativeLayout(parent);

            // Affichage du nom
            this.plugName = new TextView(parent);
            this.plugName.setTextColor(Color.BLACK);
            this.plugName.setLongClickable(true);

            lp = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.FILL_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);

            lp.addRule(RelativeLayout.CENTER_VERTICAL);

            // Ajout du label dans le conteneur gŽnŽral
            this.plug.addView(this.plugName, lp);

            // Bouton delete
            this.delete = new Button(parent);
            this.delete.setId(StepInterface.BUTTON_DELETE);
            this.delete.setBackgroundResource(R.drawable.ic_bt_del);
            this.delete.setClickable(true);
            this.delete.setOnClickListener(parent._controller);

            // Bouton Play
            this.actionText = new TextView(parent);
            this.actionText.setId(StepInterface.ACTION_TEXT);

            // Param�tres du layout pour le bouton Scenario
            lp = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);

            lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,
                    this.actionText.getId());

            // Ajout du bouton play au conteneur
            this.plug.addView(this.delete, lp);

            // Param�tres du layout pour le bouton reccurence
            lp = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);

            lp.addRule(RelativeLayout.CENTER_VERTICAL);
            lp.addRule(RelativeLayout.LEFT_OF, this.delete.getId());
            lp.rightMargin = 7;

            this.plug.addView(this.actionText, lp);

            this.addView(this.plug, new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.FILL_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT));

            this.setTexts(false);
            
            
            parent.registerForContextMenu(this);
        }
        
        /**
         * Permet de dŽfinir les textes des plugs et actions
         * Si param = true, on rafraichit les valeurs des champs
         * @param refresh
         */
        private void setTexts(boolean refresh) {
            Log.d("SAA::AI::refresh","START");
            this.plugName.setText("test");
//            this.actionText.setText(this.step.getAction() ?
//                    this.getContext().getString(
//                            R.string.add_scenario_plug_action_play) :
//                    this.getContext().getString(
//                            R.string.add_scenario_plug_action_stop));
//
//            if(refresh) {
//                this.plugName.postInvalidate();
//                this.actionText.postInvalidate();
//                
//            }

            Log.d("SAA::AI::refresh","END");
        }
    }
	
	public void addAction() {
        Log.d("SAA::addAction","START");
        
        StepInterface a = new StepInterface(this, new Step(this)); 

        this._list_comments.addView(a);
        

       
        Log.d("SAA::addAction","END");
    }
}
